<template>
  <div>
    <van-nav-bar
      title="注册"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="content">
      <van-form @submit="onSubmit">
        <van-field
          v-model="userinfo.userName"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="userinfo.passWord"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit"
            >注册</van-button
          >
          <p class="register" @click="$router.push('/login')">有账号,请登录</p>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { register } from '@api/home'
import { Toast } from 'vant'
export default {
  name: 'register',
  data () {
    return {
      userinfo: {
        userName: '',
        passWord: ''
      }
    }
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    onSubmit () {
      // 注册
      register(this.userinfo).then((res) => {
        if (res.data.code !== 201) {
          if (res.data.status === 403) {
            this.$toast.fail('用户已存在')
          }
        } else {
          this.$toast.success('注册成功')
          this.$router.push('/login')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  margin-top: 20px;
}
.register{
  margin-top: 20px;
  text-align: right;
  color:blue;
  font-size: 13px;
}
</style>
